package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.dto.CProjectApplicationDTO;
import org.jeecg.modules.demo.crm.dto.CProjectApplicationSimpleDTO;
import org.jeecg.modules.demo.crm.entity.*;
import org.jeecg.modules.demo.crm.mapper.*;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.jeecg.modules.demo.crm.service.ICProjectApplicationService;
import org.jeecg.modules.demo.crm.service.ICProjectApplicationSimpleService;
import org.jeecg.modules.demo.crm.service.ICProjectStatusLogService;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 项目信息
 * @Author: jeecg-boot
 * @Date:   2025-05-28
 * @Version: V1.0
 */
@Service
public class CProjectApplicationSImpleServiceImpl extends ServiceImpl<CProjectApplicationSimpleMapper, CProjectApplicationSimple> implements ICProjectApplicationSimpleService {

    @Autowired
    CSubProjectMapper cSubProjectMapper;

    @Autowired
    CProjectApplicationSimpleMapper cProjectApplicationMapper;

    @Autowired
    private CProjectStatusLogMapper cProjectStatusLogMapper;

    @Autowired
    CPaymentRequestMapper cPaymentRequestMapper;

    @Autowired
    CInvoiceApplyMapper cInvoiceApplyMapper;

    @Autowired
    CPaymentInvoiceMapper cPaymentInvoiceMapper;

    @Autowired
    CContractMapper cContractMapper;

    @Autowired
    CLibraryMapper cLibraryMapper;

    @Autowired
    CContractingUnitMapper cContractingUnitMapper;

    @Autowired
    CCompanyInfoMapper cCompanyInfoMapper;

    @Autowired
    ICCommonService iCommonService;



    @Override
    public CProjectApplicationSimpleDTO getOrderInfo(String id, LoginUser sysUser) {
        Boolean permissionCheck = iCommonService.permissionCheck();
        List<String> subProjectIds = null;
        if(permissionCheck){
            String projectId = cSubProjectMapper.projectPermissions(sysUser.getId());
            if(projectId == null || !projectId.contains(id)){
                throw new JeecgBootException("无权限查看");
            }
            String subProjectId = cSubProjectMapper.subProjectPermissions(sysUser.getId());
            if(subProjectId == null){
                subProjectId = "-1";
            }
            subProjectIds = Arrays.stream(subProjectId.split(","))
                    .map(String::trim)
                    .filter(StringUtils::isNotBlank)
                    .map(String::valueOf)
                    .collect(Collectors.toList());
        }

        CProjectApplicationSimple projectApplication = cProjectApplicationMapper.selectById(id);
        if(projectApplication == null){
            throw new JeecgBootException("总项目不存在");
        }
        CProjectApplicationSimpleDTO projectApplicationDTO = new CProjectApplicationSimpleDTO();
        BeanUtils.copyProperties(projectApplication, projectApplicationDTO);


        List<CSubProject> subProjectList = cSubProjectMapper.findByProjectIdAndId(projectApplicationDTO.getId(),subProjectIds);
        if(subProjectList == null){
            throw new JeecgBootException("项目不存在");
        }
        //查询库
        CLibrary cLibrary = cLibraryMapper.selectById(projectApplication.getLibraryId());
        projectApplicationDTO.setLibrary(cLibrary);

        //发包单位
        CContractingUnit cContractingUnit = cContractingUnitMapper.selectById(projectApplication.getContractingUnit());
        projectApplicationDTO.setContractingUnitVo(cContractingUnit);

        //总包主体
        CCompanyInfo cCompanyInfo = cCompanyInfoMapper.selectById(projectApplication.getCompanyInfoId());
        projectApplicationDTO.setCompanyInfo(cCompanyInfo);


        //子项目
        projectApplicationDTO.setCSubProjectList(subProjectList);

        //登陆者信息
        LoginUser sysUserData = new LoginUser();
        sysUserData.setId(sysUser.getId());
        sysUserData.setUsername(sysUser.getUsername());
        sysUserData.setRoleCode(sysUser.getRoleCode());
        projectApplicationDTO.setSysUser(sysUserData);

        //子项目合同金额（含税）全部
        BigDecimal subProjectAmount = cContractMapper.contractSubProjectPriceBySubProjectId(projectApplication.getId(),subProjectIds);
        projectApplicationDTO.setSubProjectAmount(subProjectAmount);

        //付款总金额
        BigDecimal paymentTotal = cPaymentRequestMapper.getTotalAmountBySubProjectid(projectApplication.getId(),subProjectIds);
        projectApplicationDTO.setPaymentTotal(paymentTotal);

        //收票金额
        BigDecimal receiptsTotal = cPaymentInvoiceMapper.getTotalAmountBySubProjectId(projectApplication.getId(),subProjectIds);
        projectApplicationDTO.setReceiptsTotal(receiptsTotal);

        //发票合计
        BigDecimal totalAmountWithTax = cPaymentInvoiceMapper.getTotalAmountWithTaxBySubProjectId(projectApplication.getId(),subProjectIds);
        projectApplicationDTO.setReceiptsAndTaxTotal(totalAmountWithTax);

        return projectApplicationDTO;


    }


}
